/*
    api:"修改用户信息"
    name:"陈小洋"
    time：2021.1.12
* */

const User = require('../../../control/User/User');
const tools = require('../../../tools/public');
/**
 * @api {post} /user/updUserInfo 4.更新用户信息
 * @apiGroup User
 *
 * @apiHeader {String} authentication token
 * @apiParamExample {json} Request-Example
 *{
 *   "authentication":"GmrahMAJrjBQwX6cRD5mPjpTN4w79Eep"
 *}
 *
 * @apiParam {String} UserId 用户id
 * @apiParam {String} [NickName] 昵称
 * @apiParam {String} [Avatar] 头像
 * @apiParam {String} [Introduce] 简介
 * @apiParam {Number} [sex] 性别（1男，2女）
 * @apiParam {Number} [birth] 生日
 * @apiParam {String} [IsAbleUse] 是否被禁用
 * @apiParam {Number} [RemainingLogins] 剩余登录次数
 * @apiParam {String} [Identity] 身份
 * @apiParamExample {json} Request-Example
 *{}
 * @apiSuccessExample  {json} Response-Example
 * {
 *   "code": 200,
 *   "msg": "修改用户信息成功！",
 *   "data": {
            "_id": "600ada086f506451a49231f0",
            "Account": "15892023484",
            "Token": "7B5ttrdsknmEbFBXWKPEizhWrp5Ypc7D",
            "NickName": ""
        }
 * }
 */
module.exports = async function (req, res, next) {
    let {UserId} = req.body;
    let result = null;
    let {authentication} = req.headers;

    // todo 限制sex和birth和RemainingLogins必须是数字
    try {
        if (!authentication || !UserId) return res.send(tools.ReturnData(204, '参数错误,请认真阅读接口文档！'));

        /******    处理参数为空的数据 不传给数据库    ******/
        let SelJson = {}
        let celue = ['NickName', 'Avatar', 'Introduce', 'sex', 'birth', 'Region',
            'IsAbleUse','RemainingLogins','Identity']
        for (let i in celue) {
            req.body[celue[i]] ? SelJson[celue[i]] = req.body[celue[i]] : ""
        }

        result = await User.Sel_quoteType('_id', UserId, {LoginIp: 0})

        if (result.length >= 1) {
            await User.Upd(UserId,SelJson)
            res.send(tools.ReturnData(200, '修改用户信息成功！'));
        } else {
            res.send(tools.ReturnData(204, '该账号不存在！'));
        }
    } catch (e) {
        console.log(e)
        return res.send(tools.ReturnData(500, '系统内部错误！'));
    }
};